<?php 
class kdtrace_ups extends Controller {
	
	function track( $tracknum , $lang = "en"){
		if($lang=="ch"){$langset = "zh_CN";} else {$langset = "en_CN";};
		$getdata = "HTMLVersion=5.0&loc=".$langset."&Requester=UPSHome&WBPM_lid=homepage%2Fct1.html_pnl_trk&trackNums=" . $tracknum . "&track.x=Track";
		$url 	 = "http://wwwapps.ups.com/WebTracking/track" ;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $getdata);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/x-www-form-urlencoded',
			'Referer: http://www.ups.com/content/cn/en/index.jsx',
			'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' 
		));

		$result = curl_exec($ch);
        curl_close($ch);
		$rtndata = array('tracknum'=>$tracknum,'header'=>array(),'err'=>array(),'eventList'=>array());

		if( strpos($result,"Please verify your information and try again later")>0){
			$rtndata['err']['errcode'] = '1';
			return $rtndata;
		}
		$rtndata['err']['errcode'] = '0';
		$w1 = strpos($result , '<input type="hidden" name="trackNums" value="' . $tracknum . '">');
		$w2 = strpos($result , '<div class="secLvl gradient gradientGroup7 clearfix">', $w1);
		$rhtm = substr($result , $w1 , $w2 - $w1);
		$rhtm = str_replace("\n" , "" , $rhtm);
		$rhtm = str_replace("\r" , "" , $rhtm);
		$rhtm = str_replace("	" , "" , $rhtm);
		$rhtm = str_replace("&nbsp;" , " " , $rhtm);
		$rhtm = str_replace("&amp;" , "&" , $rhtm);
		
		$patten = array(
			'zh_CN'=>'/>寄存地址：<\/label><\/dt><dd>(.+?)<\/dd>/i',
			'en_CN'=>'/>Delivered On:<\/label><\/dt><dd>(.+?)<\/dd>/i'
		);
		if(preg_match($patten[$langset], $rhtm, $matches2) == 1){
			$rtndata['header']['isDelivered']	= true;
			$patten2 = array(
				'zh_CN'=>'/>签收人：<\/label><\/dt><dt>(.+?)<\/dt>/i',
				'en_CN'=>'/>Signed By:<\/label><\/dt><dt>(.+?)<\/dt>/i'
			);
			if(preg_match($patten2[$langset], $rhtm, $matches2) == 1){
				$rtndata['header']['SignedBy']      = $matches2[1];
			};
		};

		$w1 = strpos($rhtm , 'class="dataTable">' );
		$w2 = strpos($rhtm , '</table>' ,$w1);
		$bdhtm = substr($rhtm , $w1 , $w2 - $w1);
		preg_match_all('/<td class\="nowrap">(.+?)<\/td>\s*<td class\="nowrap">(.+?)<\/td>\s*<td class\="nowrap">(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<\/tr>/i', $bdhtm, $matches);
		
		$count = count($matches[0]);
		if($count>0){
			for($i=0 ; $i < $count ; $i++){
				//$this->skipSpaces($matches[1][$i])
				$tt =  strtotime( $this->skipSpaces($matches[2][$i]) . " " . $this->skipSpaces($matches[3][$i]) );
				$rtndata['eventList'][$i] = array(
					"date"			=> date('Y-m-d',$tt),
					"time"			=> date('h:i:s',$tt),
					"utime"			=> $tt,
					"status"		=> $this->skipSpaces($matches[4][$i]),
					"location"		=> $this->skipSpaces($matches[1][$i])
				);
			}
		}

		$rtndata['eventList'] = array_reverse( $rtndata['eventList'] );
		return $rtndata;
	}
	function skipSpaces($t){
			return  trim(preg_replace('/\s{2,}/', ' ', $t));
	}
	
}

?>